package com.ftg.learn;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;
import org.springframework.util.DigestUtils;
import sun.security.provider.MD5;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.math.BigInteger;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collections;

public class MybatisGenerator {

    @Test
    public void createMybatis(){
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/rbac", "root", "Love0913@kjx")
                .globalConfig(builder -> {
                    builder.author("kangjx") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir("D:\\workspace\\java\\learn-springboot\\src\\main\\java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.ftg.learn") // 设置父包名
                            .moduleName("rbac"); // 设置父包模块名
                })
                .strategyConfig(builder -> {
                    builder.addInclude("sys_permission","sys_role","sys_user") // 设置需要生成的表名
                            .addTablePrefix("sys_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

    @Test
    public void createUserInfo(){
        // 数据加密 md5
        try {
            byte[] bs1 = MessageDigest.getInstance("md5").digest("admin".getBytes());
            System.out.println(Arrays.toString(bs1));

            String password = new BigInteger(1, bs1).toString(16);
            System.out.println(password);
            //21232f297a57a5a743894a0e4a801fc3

            String str = DigestUtils.md5DigestAsHex("admin".getBytes());
            System.out.println(str);


            System.out.println("====>jdk 自带的des对称加密算法===>");
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
            keyGenerator.init(56);//初始化此密钥生成器,使其具有确定的密钥大小
            SecretKey secretKey = keyGenerator.generateKey();//生成一个密钥
            byte[] bs = secretKey.getEncoded();

            // key转换
            DESKeySpec desKeySpec = new DESKeySpec(bs); //实例化DES密钥规则
            SecretKeyFactory factory = SecretKeyFactory.getInstance("DES"); //实例化密钥工厂
            Key convertSecretKey = factory.generateSecret(desKeySpec); //生成密钥

            // 加密
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
            byte[] result = cipher.doFinal("admin".getBytes());

            System.out.println(Arrays.toString(result));
            System.out.println(new String(result));
//            System.out.println("jdk des encrypt:" + Hex.encodeHexString(result));

            // 解密
            cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
            result = cipher.doFinal(result);
            System.out.println("jdk des decrypt:" + new String(result));
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

}
